/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import java.security.SecureRandom;

/**
 *
 * @author admin
 */
public class SpaceBuddie {
     private int x;
     private int y;
     
     private SpriteBatch batch;
     private Texture texture;
     private Sprite sprite;
     private final String buddieSprite = "space-baddie.png";
     private Baddies [] baddies        = new Baddies[5];
     
     // load sprite for exploding space buddie
    private static final int   FRAME_COLS = 5;        
    private static final int   FRAME_ROWS = 1;         
    private Texture explosionTexture;
    private final String explosionSprite = "sbsplit.png";
    private float            stateTime;
    private Animation        explosion;
    private TextureRegion    currentFrame;
    private  TextureRegion[] explosionFrames;
     // end of loading
    
    private boolean allVisible =  true;
    private Rectangle bounds;
    
     public SpaceBuddie(){
       Texture.setEnforcePotImages(false);
       batch     = new SpriteBatch();
       texture   = new Texture(Gdx.files.internal(buddieSprite));
       x         = 40;
       y         = 30;
       sprite = new Sprite(texture);
       sprite.setScale(1.5f);
       bounds = new Rectangle();
       bounds.height = sprite.getHeight();
       bounds.width  = sprite.getWidth();
       for(int i =0; i<5; i++){
           Baddies bb = new Baddies();
           bb.setSprite(sprite);
           bb.setX( new SecureRandom().nextInt(470));
           //bb.setY(new SecureRandom().nextInt(420));
           bb.setY(420);
           bounds.x = bb.getX();
           bounds.y = bb.getY();
           bb.setBounds(bounds);
           float sp = new SecureRandom().nextInt(2) + 0.5f;
           if( sp < 1.0f) sp = 1.5f;
           bb.setSpeed(sp);
           baddies[i] = bb;
       }
       
       // initialize the explode buddies
        Texture.setEnforcePotImages(false);
        explosionTexture   = new Texture(Gdx.files.internal(explosionSprite));
        explosionFrames    = new TextureRegion[FRAME_COLS * FRAME_ROWS];
      TextureRegion[][] tmp = TextureRegion.split(explosionTexture, explosionTexture.getWidth() / FRAME_COLS, explosionTexture.getHeight() / FRAME_ROWS);
      int index = 0;
      for (int i = 0; i < FRAME_ROWS; i++) {
               for (int j = 0; j < FRAME_COLS; j++){
                    explosionFrames[index++] = tmp[i][j];
               }
      }
     // explosion = new Animation(0.025f, explosionFrames);  
     explosion = new Animation(1.0f, explosionFrames);  
     stateTime = 0f;
     }
 
     public void drawSpaceBuddie(SpriteBatch batch){
      // check for all visile.If all not visible,  then initialize it
         for(Baddies bb : baddies){
             if(bb.visible) break;
             allVisible = false;
         }
         if(!allVisible){
             int i = 0;
             for(Baddies bb : baddies){
             bb.visible = true;
             bb.setX( new SecureRandom().nextInt(420));
             bb.setY(420);
             baddies[i] = bb;
             i++;
           }
             allVisible = true;
         }
      // end of checking 
      for(Baddies bb : baddies){
         if(bb.visible){
         if(bb.y < 0 )  bb.y = 420;
         sprite.setPosition(bb.x, bb.y);
          bb.drawSpaceBaddies(batch);
          //bb.y = bb.y - 2.0f;
          bb.y = bb.y - bb.getSpeed();
          bb.bounds.x = bb.x;
          bb.bounds.y = bb.y;
          }
        }
     }
     
     public void updateSpaceBuddies(int i, Baddies baddie){
       baddies[i] = baddie;
     }

     public Sprite getSprite(){
       return sprite;
     }
     
    public void moveBaddies(){
    }

    public Baddies [] getBuddies(){
        return baddies;
    }
    
    
    public void update(float d){
        stateTime+=d;
    }
     class Baddies {
         
         private float x;
         
         private float y;
         
         private Sprite sprite;
         
         private float speed;
         
         private Rectangle bounds;
         
         public boolean visible = true;
         public void setSprite(Sprite sprite){
           this.sprite = sprite;
         }
         
         public float getX(){
             return x;
         }
         
         public void setX(float x){
            this.x = x;
         }
         
         public void setY(float y){
            this.y = y;
         }
         
         public float getY(){
           return y; 
         }
         
         public void drawSpaceBaddies(SpriteBatch batch){
           sprite.draw(batch);              
         }
           
         public Sprite getSprite(){
           return sprite;
         }
         
        /**
         * @return the speed
         */
        public float getSpeed() {
            return speed;
        }

        /**
         * @param speed the speed to set
         */
        public void setSpeed(float speed) {
            this.speed = speed;
        }

        /**
         * @return the bounds
         */
        public Rectangle getBounds() {
            return bounds;
        }

        /**
         * @param bounds the bounds to set
         */
        public void setBounds(Rectangle bounds) {
            this.bounds = bounds;
        }
         
     }
     
     
}
